package com.tensin.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.tensin.mall.product.entity.ProductAttrValueEntity;
import com.tensin.mall.product.service.ProductAttrValueService;
import com.tensin.mall.product.vo.AttrRespVo;
import com.tensin.mall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.tensin.mall.product.entity.AttrEntity;
import com.tensin.mall.product.service.AttrService;
import com.tensin.common.utils.PageUtils;
import com.tensin.common.utils.R;



/**
 * 商品属性
 *
 * @author tensin
 * @email 56793079@qq.com
 * @date 2022-07-11 21:52:06
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;


    /**
     * /product/attr/base/listforspu/{spuId}
     * 获取spu规格 回显
     * @param spuId
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrlistforspu(spuId);
        return R.ok().put("data",entities);
    }


    /**
     *  规格参数页面查询功能
     *  http://localhost:88/api/product/attr/base/list/0?t=1659110239870&page=1&limit=10&key=
     *  销售查询：
     *  http://localhost:88/api/product/attr/sale/list/0?t=1659195367902&page=1&limit=10&key=
     *  这两个功能的查询课公用，判断中间是base还是sale。如果是base则是基础属性，attr_type为0.sale则是小手术形，attr_type为1
     * @param params 封装来的数据
     * @param catelogId 地址传来的三级分类id
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type){

        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     * 用于平台规格-属性参数修改按钮回显
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo respVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", respVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * /product/attr/update/{spuId}
     * 商品系统-修改商品规格
     */
    @PostMapping("/update/{spuId}")
    public R update(@PathVariable("spuId") Long spuId,
                    @RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId,entities);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
